﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Derbster.Domain
{
    public class CommandManager
    {
        private static Stack<Command> _undoCommands;
        private static Stack<Command> _redoCommands;

        public CommandManager()
        {
            if (_undoCommands == null)
                _undoCommands = new Stack<Command>();

            if (_redoCommands == null)
                _redoCommands = new Stack<Command>();
        }

        public void Add(Action undoAction, Action redoAction)
        {
            Contract.Requires(undoAction != null);
            Contract.Requires(redoAction != null);
            Add(new Command(redoAction,undoAction));
        }

        public void Add(Command command)
        {
            _undoCommands.Push(command);
        }

        public void Clear()
        {
            _undoCommands.Clear();
            _redoCommands.Clear();
        }

        public void Redo()
        {
            if (_redoCommands.Count != 0)
            {
                var command = _redoCommands.Pop();
                command.Redo();
                _undoCommands.Push(command);
            }
        }
        public void Undo()
        {
            if (_undoCommands.Count != 0)
            {
                var command = _undoCommands.Pop();
                command.Undo();
                _redoCommands.Push(command);
            }
        }
    }
}
